package in.muton.edushare.pages.common;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.newentities.NewCourseCreator;
import in.muton.edushare.pages.EduShareBasePage;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.Departments;

public class NewCoursePanel extends EdusharePanel{

	private static final long serialVersionUID = -8512148163777232181L;

	public NewCoursePanel(String id, final IModel<Departments> model, EduShareBasePage page) {
		super(id, model,page);
		WebMarkupContainer wmc = new WebMarkupContainer("msgContainer") {
			@Override
			public boolean isVisible() {
				if (getStatusList().size() > 0)
					return true;
				return false;
			}

			private static final long serialVersionUID = 6674357829244135004L;

		};
		add(wmc);
		wmc.add(new ListView<String>("message",
				new PropertyModel<List<String>>(this, "statusList")) {
			private static final long serialVersionUID = -6031452532751464514L;

			@Override
			protected void populateItem(ListItem<String> arg0) {
				arg0.add(new Label("string", arg0.getModel()).setOutputMarkupId(true));
			}
		});
		Form<Void> f;
		add(f = new Form<Void>("newCourse") {
			@Override
			public void onSubmit() {
				if(!StringUtil.isValid(getNewCourse())){
					setError("Edushare needs a valid course name to start the course.");
					return;
				}
				statusList = new NewCourseCreator(EduShareSession.get()
						.getSessionORMInterface(), model)
						.addCoursesToDept(getNewCourse());
				setNewCourse("");
			}

			private static final long serialVersionUID = 1L;
		});
		f.add(new TextArea<String>("courseNames", new PropertyModel<String>(
				this, "newCourse")));

	}

	private String newCourse = "";
	private List<String> statusList = new ArrayList<String>();

	public String getNewCourse() {
		return newCourse;
	}

	public void setNewCourse(String newDept) {
		this.newCourse = newDept;
	}

	public List<String> getStatusList() {
		return statusList;
	}

	public void setStatusList(List<String> statusList) {
		this.statusList = statusList;
	}
}
